package com.example.contentprovidertest;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyProvider extends ContentProvider {
    MyDBHelper dbHelper;
    @Override
    public boolean onCreate() {
        dbHelper=new MyDBHelper(getContext());
        return true;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        db.insert(MyDBHelper.myProvider_table_name, "id", values);
        return null;
    }

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        int id=db.delete(MyDBHelper.myProvider_table_name, arg1, arg2);
        if(id!=0)return id;
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        int id=db.update(MyDBHelper.myProvider_table_name, values, selection, selectionArgs);
        if(id!=0)return id;
        return 0;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        Cursor c=db.query(MyDBHelper.myProvider_table_name,projection, selection, selectionArgs, null, null, sortOrder);
        return c;
    }
}
